import { request, config } from 'utils'
import * as commonService from 'utils/services'

const {api} = config
const {organization, security} = api

/**
 * 组织机构树
 */
export async function tree () {
  const url = `${organization}/getTree`
  return request({url: url, method: 'GET'})
}

/**
 * 添加
 * @param params
 * @return {Promise<*>}
 */
export async function store (params) {
  const url = `${organization}/create`
  return request({url: url, method: 'POST', data: params})
}

/**
 * 修改
 * @param params
 * @return {Promise<*>}
 */
export async function update (params) {
  const url = `${organization}/update`
  return request({url: url, method: 'PUT', data: params})
}

/**
 * 删除
 * @param id
 * @param version
 * @return {Promise<*>}
 */
export async function destroy (id, version) {
  const url = `${organization}/delete/${id}/${version}`
  return request({url: url, method: 'POST'})
}

/**
 *
 * @param params
 * @return {Promise<*>}
 */
export async function bindUser (params) {
  const url = `${organization}/bindUser`
  return request({url: url, method: 'PUT', data: params})
}

/**
 *
 * @param params
 * @return {Promise<*>}
 */
export async function unbindUser (params) {
  const url = `${organization}/unbindUser`
  return request({url: url, method: 'PUT', data: params})
}

/**
 * 获取已经绑定的userIds
 * @param id
 * @return {Promise<*>}
 */
export async function getUserIds (id) {
  const url = `${organization}/listUserId/${id}`
  return request({url: url, method: 'GET'})
}

export async function getTenantBoundUserIds (id = '') {
  const url = `${organization}/listTenantBoundUserIds`
  return request({url: url, method: 'GET'})
}

/**
 * 排序
 * @param pid
 * @param params
 * @returns {Promise<*>}
 */
export async function sortAs (pid, params) {
  const url = `${organization}/${pid}/sortAs`
  return request({url: url, method: 'POST', data: params})
}

export async function getUserOrganizations (userId) {
  const url = `${organization}/listOrg/${userId}`
  return request({ url: url, method: 'GET'})
}

/**
 * 为用户绑定组织机构
 * @param userId
 * @param params
 * @returns {Promise<*>}
 */
export async function bindOrgs (userId, params) {
  const url = `${organization}/bindOrgs/${userId}`
  return request({ url: url, method: 'PUT', data: params})
}
